// @author A0096579E

package rightnow.storage;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import com.eaio.uuid.UUID;

/**
 * Task is an Object that stores all information related to a real life task.
 * Each Task is assigned a unique identification number upon being added into 
 * Storage.
 */
public class Task {
	
	private UUID taskId;
	
	private String description;
	
	private LocalDate startDate;
	private LocalDate endDate;
	private LocalTime startTime;
	private LocalTime endTime;
	
	private boolean isPinned;
	private boolean isDone;
	
	public Task(String description) {
		this.description = description;
		this.isPinned = false;
		this.isDone = false;
	}
	
	Task(Task task) {
		this.taskId = task.getTaskId();
		this.description = task.getDescription();
		this.startDate = task.getStartDate();
		this.endDate = task.getEndDate();
		this.startTime = task.getStartTime();
		this.endTime = task.getEndTime();
		this.isPinned = task.isPinned();
		this.isDone = task.isDone();
	}
	
	/*
	 * These methods are getters and setters
	 */

	public UUID getTaskId() {
		return taskId;
	}
	
	void setTaskId(UUID taskId) {
		this.taskId = taskId;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public LocalDate getStartDate() {
		return startDate;
	}

	public void setStartDate(LocalDate startDate) {
		this.startDate = startDate;
	}

	public LocalDate getEndDate() {
		return endDate;
	}

	public void setEndDate(LocalDate endDate) {
		this.endDate = endDate;
	}

	public LocalTime getStartTime() {
		return startTime;
	}

	public void setStartTime(LocalTime startTime) {
		this.startTime = startTime;
	}

	public LocalTime getEndTime() {
		return endTime;
	}

	public void setEndTime(LocalTime endTime) {
		this.endTime = endTime;
	}

	public boolean isPinned() {
		return isPinned;
	}

	public void setPinned(boolean isPinned) {
		this.isPinned = isPinned;
	}

	public boolean isDone() {
		return isDone;
	}

	public void setDone(boolean isDone) {
		this.isDone = isDone;
	}
	
	public DateTime getStart() {
		return convertToDateTime(this.startDate, this.startTime);
	}
	
	public DateTime getEnd() {
		return convertToDateTime(this.endDate, this.endTime);
	}
	
	private DateTime convertToDateTime(LocalDate date, LocalTime time) {
		if (date == null || time == null) {
			return null;
		} else {

			int year = date.getYear();
			int month = date.getMonthOfYear();
			int day = date.getDayOfMonth();

			int hour = time.getHourOfDay();
			int minute = time.getMinuteOfHour();
			int second = time.getSecondOfMinute();
			int millisecond = time.getMillisOfSecond();
			
			return new DateTime(year, month, day, hour, minute, second, millisecond);
		}
	}
	
	public boolean contentEquals(Task task) {
		if (task == null) {
			return false;
		}
		else if (this.description != null){
			if (!this.description.equals(task.getDescription())) {
				return false;
			}
		}
		else if (this.getStart() != null) {
			if (this.getStart().equals(task.getStart())) {
				return false;
			}
		}
		else if (this.getEnd() != null) {
			if (!this.getEnd().equals(task.getEnd())) {
				return false;
			}
		}
		else if (this.isDone != task.isDone()) {
			return false;
		}
		else if (this.isPinned != task.isPinned()) {
			return false;
		}
		return true;
	}
	
	/*
	 * These methods are used to check if Task has valid time attributes
	 */
	
	public boolean isValid() {
		if (hasValidDateTime() && hasValidStartEnd()) {
			return true;
		} else {
			return false;
		}
	}
	
	private boolean hasValidDateTime() {
		if (this.startDate == null && this.startTime != null) {
			return false;
		}
		if (this.endDate == null && this.endTime != null) {
			return false;
		}
		if (this.startDate != null && this.startTime == null) {
			return false;
		}
		if (this.endDate != null && this.endTime == null) {
			return false;
		}
		return true;
	}
	
	private boolean hasValidStartEnd() {
		DateTime start = getStart();
		DateTime end = getEnd();
		
		if ((start != null && end != null) && start.compareTo(end) > 0) {
			return false;
		}
		return true;
	}
	
}
